package com.seio.edc.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.model.util.Location;
import com.nova.frame.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2016/1/20 0020.
 */
public class BmapUtil {

    public static JSONObject getOutput(String url) {
        try {
            URL url2 = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            BufferedReader bufferedReader = new
                    BufferedReader(new java.io.InputStreamReader(inputStream));
            String output = bufferedReader.readLine();
            if (StringUtils.isNotNullOrEmpty(output)) {
                JSONObject jsonObject = JSON.parseObject(output);
                return jsonObject;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Location getLocation(CustomerInfo customerInfo) {
        try {
            if (StringUtils.isNotNullOrEmpty(customerInfo.getStreet())) {
                String url = "http://api.map.baidu.com/geocoder/v2/?address=" + customerInfo.getStreet().replaceAll(" ", "") + "&output=json&ak=702632E1add3d4953d0f105f27c294b9";
                JSONObject result = getOutput(url);
                Integer status = result.getInteger("status");
                if (status == 0) {
                    Location location = JSON.parseObject(result.getJSONObject("result").getJSONObject("location").toJSONString(), Location.class);
                    return location;
                } else {
                    if (StringUtils.isNotNullOrEmpty(customerInfo.getCity())) {
                        url = "http://api.map.baidu.com/geocoder/v2/?address=" + customerInfo.getCity().replaceAll(" ", "") + "&output=json&ak=702632E1add3d4953d0f105f27c294b9";
                        result = getOutput(url);
                        status = result.getInteger("status");
                        if (status == 0) {
                            Location location = JSON.parseObject(result.getJSONObject("result").getJSONObject("location").toJSONString(), Location.class);
                            return location;
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setStreet("经十路");
        customerInfo.setCity("济南市");
        Location location = getLocation(customerInfo);
        System.out.print(location.getLat());

    }
}
